﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("advertisement")]
    public class AdvertisementController : AdminController
    {
        private IAdvertisementService _iAdvertisementService;

        public AdvertisementController(IAdvertisementService iAdvertisementService)
        {
            _iAdvertisementService = iAdvertisementService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult Gets(AdvertisementQuery query)
        {
            var model = _iAdvertisementService.GetAdvertisements(query);
            return Json(ResultToJson.Json(new { e_position_type_list = typeof(EAdvertisementPositionType).GetDropdownItemList(), data = model }), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get(long id)
        {
            advertisement_dto_ex model = new advertisement_dto_ex() { status = EAdvertisementStatus.Normal, position_type = EAdvertisementPositionType.IndexBanner, img_url = "" };
            if (id > 0)
                _iAdvertisementService.GetAdvertisement(id).ToCopyProperty(model);
            model.status_value = ((int)model.status).ToString();//ng-select 需要绑定字符串类型
            model.position_type_value = ((int)model.position_type).ToString();//ng-select 需要绑定字符串类型
            model.link_url = model.link_url ?? "";
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Add(advertisement_dto_ex dto)
        {
            dto.position_type = (EAdvertisementPositionType)int.Parse(dto.position_type_value);
            dto.status = (EAdvertisementStatus)int.Parse(dto.status_value);
            var file_url = Upload("advertisement");
            if (!string.IsNullOrEmpty(file_url))
                dto.img_url = file_url;
            else
                return Json(ResultToJson.Json(1001, "请先上传图片", new { }), JsonRequestBehavior.AllowGet);
            var result = _iAdvertisementService.AddAdvertisement(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Update(advertisement_dto_ex dto)
        {
            dto.position_type = (EAdvertisementPositionType)int.Parse(dto.position_type_value);
            dto.status = (EAdvertisementStatus)int.Parse(dto.status_value);
            var file_url = Upload("advertisement");
            if (!string.IsNullOrEmpty(file_url))
                dto.img_url = file_url;

            var result = _iAdvertisementService.UpdateAdvertisement(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "未做任何修改", result), JsonRequestBehavior.AllowGet);

        }

        [HttpDelete]
        public JsonResult Delete(long id)
        {
            var result = _iAdvertisementService.DeleteAdvertisement(id);
            return Json(ResultToJson.Json(result > 0 ? " 删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult UpdateStatus(long id, int status)
        {
            var result = _iAdvertisementService.UpdateAdvertisementStatus(id, status);
            return Json(ResultToJson.Json(result > 0 ? " 修改成功" : "修改失败", result));
        }

    }
}